1 //+-----------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // Implements the ClassFactory of PresentationHost.
12 // Ported Windows->DevDiv. See SourcesHistory.txt.
14 //------------------------------------------------------------------------
16 #include "Precompiled.hxx"
17 #include "ClassFactory.hxx"
18 #include "HostShim.hxx"
21 DWORD
CClassFactory::s_dwRegistrationToken
[] = {0,0};
22 LPCLSID
CClassFactory::s_clsIdActivated
= NULL
;
24 //******************************************************************************
26 // CClassFactory::CClassFactory
28 //******************************************************************************
30 CClassFactory::CClassFactory(__in REFCLSID clsId
)
36 //******************************************************************************
38 // CClassFactory::~CClassFactory
40 //******************************************************************************
42 CClassFactory::~CClassFactory()
47 //******************************************************************************
49 // CClassFactory::RegisterClassObjects
51 // This method creates ClassFactory objects and registers their objects based
52 // on the flags. We don't hold on to the factories; OLE has them, and when
53 // we Revoke them, OLE will Release them and they will be deleted.
54 //******************************************************************************
56 HRESULT
CClassFactory::RegisterClassObjects()
60 ASSERT(s_dwRegistrationToken
[0] == 0);
61 ASSERT(s_dwRegistrationToken
[1] == 0);
64 CClassFactory
* pFactory
= new CClassFactory(CLSID_DocObjPackage
);
66 CKHR(pFactory
->RegisterClassObject(s_dwRegistrationToken
[0]));
69 #ifdef APPLICATION_SHIM
70 CClassFactory
* pFactory
= new CClassFactory(CLSID_DocObjXaml
);
72 CKHR(pFactory
->RegisterClassObject(s_dwRegistrationToken
[0]));
74 pFactory
= new CClassFactory(CLSID_DocObjXapp
);
76 CKHR(pFactory
->RegisterClassObject(s_dwRegistrationToken
[1]));
82 CClassFactory::RevokeAllClassObjects();
88 //******************************************************************************
90 // CClassFactory::RevokeAllClassObjects
92 // Revoke any registered ClassObjects, and delete the ClassFactories.
93 // This doesn't check any return values, nor does it return anything,
94 // because this is a cleanup step, and if it fails, it really isn't
96 //******************************************************************************
98 void CClassFactory::RevokeAllClassObjects()
100 for (UINT tokenIndex
= 0; tokenIndex
< MAX_TOKENS
; ++tokenIndex
)
102 if (s_dwRegistrationToken
[tokenIndex
])
104 CoRevokeClassObject(s_dwRegistrationToken
[tokenIndex
]);
105 s_dwRegistrationToken
[tokenIndex
] = 0;
110 //******************************************************************************
112 // CClassFactory::RegisterClassObject
114 // This method registers the ClsObject that this is offering.
115 //******************************************************************************
117 HRESULT
CClassFactory::RegisterClassObject(DWORD
& dwRegistrationToken
)
121 LPUNKNOWN pUnknown
= reinterpret_cast<LPUNKNOWN
> (this);
123 //register the MIME-type handler class object
124 CKHR(CoRegisterClassObject(m_clsId
,
128 &dwRegistrationToken
));
130 if (hr
!= S_OK
&& dwRegistrationToken
)
132 CoRevokeClassObject(dwRegistrationToken
);
133 dwRegistrationToken
= 0;
138 //******************************************************************************
140 // CClassFactory::QueryInterface
142 //******************************************************************************
144 STDMETHODIMP
CClassFactory::QueryInterface(REFIID riid
,
145 __out LPVOID
*ppReturn
)
151 if (IsEqualIID(riid
, IID_IUnknown
))
153 *ppReturn
= reinterpret_cast<LPUNKNOWN
>
154 (reinterpret_cast<LPCLASSFACTORY
>(this));
158 else if (IsEqualIID(riid
, IID_IClassFactory
))
160 *ppReturn
= reinterpret_cast<LPCLASSFACTORY
>(this);
174 //******************************************************************************
176 // CClassFactory::AddRef
178 //******************************************************************************
180 STDMETHODIMP_(DWORD
) CClassFactory::AddRef()
182 return ++m_ObjRefCount
;
185 //******************************************************************************
187 // CClassFactory::Release
189 //******************************************************************************
191 STDMETHODIMP_(DWORD
) CClassFactory::Release()
193 if (--m_ObjRefCount
== 0)
199 return m_ObjRefCount
;
202 //******************************************************************************
204 // CClassFactory::CreateInstance
206 //******************************************************************************
208 STDMETHODIMP
CClassFactory::CreateInstance(__in LPUNKNOWN pUnknown
,
210 __out LPVOID
*ppObject
)
212 EventWriteWpfHostUm_ClassFactoryCreateInstance();
215 CHostShim
* pHostShim
= NULL
;
221 if (pUnknown
!= NULL
)
223 hr
= CLASS_E_NOAGGREGATION
;
227 pHostShim
= new CHostShim();
230 CKHR(pHostShim
->QueryInterface(riid
, ppObject
));
232 SetActivatedClsId(m_clsId
);
241 // Revoke any class factories that may have been created.
242 // Current implementation only creates one.
243 CClassFactory::RevokeAllClassObjects();
248 //******************************************************************************
250 // CClassFactory::LockServer
252 //******************************************************************************
254 STDMETHODIMP
CClassFactory::LockServer(BOOL fLock
)
260 InterlockedIncrement(&g_ServerRefCount
);
264 InterlockedDecrement(&g_ServerRefCount
);
266 if (g_ServerRefCount
<= 0)
268 PostQuitMessage(ERROR_SUCCESS
);